<?php 
session_start();
require_once ("class/m_product.php");
require_once ("class/m_stock.php");
$m = new M_Product();
$m_stock = new M_stock();

if (isset($_POST['m'])  &&  $_POST['m']!=""){
	if ($_POST['m']== 'view'){
		if ($_SESSION['branchtype']==1){
			$branch=$_POST['branch'];
		}else{
			$branch=$_SESSION['bid'];
		}
		$category=$_POST['category'];
		$type=$_POST['type'];
		$productname=$_POST['productname'];
		
		$page=isset($page)?$page:1;
		
		$result=$m_stock->getStockProduct($branch, $productname,$category, $type, $page,$_POST['pos'],$_POST['stock'],$_POST['premium']);
		$rows=$m_stock->getRowCount();
					
		$i=1 + (($page-1) * $m_stock->getListPage());
		$html="";
		if ($rows>0){
			while($row = $result->FetchRow()){
				If($i%2 <> 0){
					$html .= "<tr class=row1>";
				} else{
					$html .= "<tr>";    
				}
				$html .= getTableDetail($row,$i)."</tr>";
				$i++;
			}
		}else{
				$html="<tr>"
				."<td colspan='16' align='center' ><img src='./images/info.gif'/><b/></td>"
				."</tr>";
		}
		//Return result , rows , page
		$ar_result=array('data'=>$html, 	
						'rows'=>$rows,
						'totalpage'=>$m_stock->getTotalPage());
		echo  json_encode( $ar_result );
		
		
	}else if($_POST['m']== 'print'){
		if ($_SESSION['branchtype']==1){
				$branch=$_POST['branch'];
			}else{
				$branch=$_SESSION['bid'];
			}
			$category=$_POST['category'];
			$type=$_POST['type'];
			$productname=$_POST['productname'];
			
			$page=isset($page)?$page:1;
			
			$result=$m_stock->getStockProduct($branch, $productname,$category, $type, $page,$_POST['pos'],$_POST['stock'],$_POST['premium']);
			$rows=$m_stock->getRowCount();
						
			$i=1 + (($page-1) * $m_stock->getListPage());
			$html="";
			if ($rows>0){
				while($row = $result->FetchRow()){
					If($i%2 <> 0){
						$html .= "<tr class=row2>";
					} else{
						$html .= "<tr>";    
					}
					$html .= getTableDetail2($row,$i)."</tr>";
					$i++;
				}
			}else{
					$html="<tr>"
					."<td colspan='14' align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			//Return result , rows , page
			$ar_result=array('data'=>$html, 	
							'rows'=>$rows,
							'totalpage'=>$m_stock->getTotalPage());
			echo  json_encode( $ar_result );
	}else if($_POST['m']== 'update'){
		$Amt=$_POST['amt'];
		$ProductID=$_POST['pid'];
		$BranchID=$_POST['bid'];
		$row=$_POST['rows'];
		$m_stock->StockUpdate($ProductID,$BranchID,$Amt,$_SESSION['username']);
		
		echo getTableDetail($m_stock->getStockByID($ProductID,$BranchID), $row);
	}
}else{
	if (isset($_GET['c']) && $_GET['c'] !="" ){
		echo json_encode( $m->getDataCBO($_GET['c']));
	}
}

	function getTableDetail($row,$i){
		global $stockScreen;
		
		$stock=$row["IsStock"]==1?"<img src='images/success.png' border='0' />":"&nbsp;";
		$premium=$row["IsPremium"]==1?"<img src='images/success.png' border='0' />":"&nbsp;";
		$POS=$row["IsPOS"]==1?"<img src='images/success.png' border='0' />":"&nbsp;";
		
		$html="<td align='right'>".$i."</td>";
		$html.="<td style='display:none'>".$row["BranchID"]."</td>";
		$html.="<td style='display:none'>".$row["ProductID"]."</td>";
		
        if ($_SESSION['branchtype']==1){
			$html .="<td align='left'>".$row['BranchName']."</td>";
		}
        
		$color="";
		if ($row['SubUnit']=='' || $row['SubUnit']==$row['Unit'] ){
			$subunit= '';
			$amt=number_format($row['Amount'],'0');
            $mamt=number_format($row['Amount'],'0');
			$total = $row['Amount'] * $row['Cost_branch'];
			
			if ($row['Amount'] < $row["LimitNumber"])
				$color="style='color:red'";		
                	
		}else{
			$subunit= "-(".$row['Scale'] .' '.$row['SubUnit'].")";
			$amt=number_format( $row['mAmount'],'0','.',',')." [".number_format($row['Amount'],'0')."]";
          $mamt= number_format( (($row['Amount']) - ($row['Amount'] % $row['Scale'] )) / $row['Scale'],'0','.',',') ;
			$total= ($row['Cost_branch']/$row['Scale'] * $row['Amount']);
			if ($row['mAmount'] < $row["LimitNumber"])
				$color="style='color:red'";
		}

		
		$html .="<td align='left'>".$row['ProductName']."</td>";
		$html .="<td  align='left'>".$row['CatName']."</td>";
		// $html .="<td  align='center'>".$row['ProductType']."</td>";
		$html .="<td  align='center'>".$POS."</td>";
		$html .="<td  align='center'>".$stock."</td>";
		$html .="<td  align='center'>".$premium."</td>";
		$html .="<td  align='center'>".$row['Unit']."".$subunit."</td>";
		$html .="<td  align='right'>".number_format( $row['Cost_branch'] ,'2','.',',')."</td>";
        $html .="<td  align='right' ".$color.">".$mamt."</td>";	
		$html .="<td  align='right' ".$color.">".$amt."</td>";	
		$html .="<td  align='right'>".number_format( $total ,'2','.',',')."</td>";	
		if ($_SESSION["permission"][$stockScreen]["Edit"]=="1"){
			$html .="<td  align='right'><input type='textbox' style='text-align:right;font-size:11px;border:1px solid #ccc;padding:2px;' name='txtstock' maxlength='7' size='5'  value='".$row['Amount']."'/></td>";
			$html .="<td  align='center'><img src='images/save24.png' style='cursor:pointer' title='Save amount' class='btnSave' border='0' /></td>";
			
		}
		return $html;	
	}
	
	function getTableDetail2($row,$i){
	 
		$stock=$row["IsStock"]==1?"Yes":"No";
		$premium=$row["IsPremium"]==1?"Yes":"No";
		$POS=$row["IsPOS"]==1?"Yes":"No";
		
		$html="<td align='right'>".$i."</td>";
		$html.="<td style='display:none'>".$row["BranchID"]."</td>";
		$html.="<td style='display:none'>".$row["ProductID"]."</td>";
        
		if ($_SESSION['branchtype']==1){
			$html .="<td align='left'>".$row['BranchName']."</td>";
		}
		$color="";
		if ($row['SubUnit']=='' || $row['SubUnit']==$row['Unit'] ){
			$subunit= '';
			$amt=number_format($row['Amount'],'0');
                $mamt=number_format($row['Amount'],'0');
			$total = $row['Amount'] * $row['Cost_branch'];
			
			if ($row['Amount'] < $row["LimitNumber"])
				$color="style='color:red'";			
		}else{
			$subunit= "-(".$row['Scale'] .' '.$row['SubUnit'].")";
			$amt=number_format( $row['mAmount'],'0','.',',')." [".number_format($row['Amount'],'0')."]";
             $mamt= number_format( (($row['Amount']) - ($row['Amount'] % $row['Scale'] )) / $row['Scale'],'0','.',',') ;
			$total= ($row['Cost_branch']/$row['Scale'] * $row['Amount']);
			if ($row['mAmount'] < $row["LimitNumber"])
				$color="style='color:red'";
		}

		$html .="<td align='left'>".$row['ProductName']."</td>";
		$html .="<td  align='left'>".$row['CatName']."</td>";
		// $html .="<td  align='center'>".$row['ProductType']."</td>";
		$html .="<td  align='center'>".$POS."</td>";
		$html .="<td  align='center'>".$stock."</td>";
		$html .="<td  align='center'>".$premium."</td>";
		$html .="<td  align='center'>".$row['Unit']."".$subunit."</td>";
		$html .="<td  align='right'>".number_format( $row['Cost_branch'] ,'2','.',',')."</td>";
        $html .="<td  align='right' ".$color.">".$mamt."</td>";		
		$html .="<td  align='right' ".$color.">".$amt."</td>";	
		$html .="<td  align='right'>".number_format( $total ,'2','.',',')."</td>";	
		 
		return $html;	
	}
?>